package com.zimin.redis.aop.spring;

import com.zimin.common.exception.GraceException;
import com.zimin.shared.enums.ResponseStatusEnum;
import com.zimin.shared.pojo.Result;
import com.zimin.common.utils.ObjectUtils;
import com.zimin.shared.annotation.Idempotence;
import com.zimin.redis.utils.RedisOperator;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import reactor.core.publisher.Mono;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zimin
 * @since 2021/9/28 3:37 下午
 */
//@Component
@Slf4j
@Deprecated
public class IdempotenceInterceptor implements MethodInterceptor {

    //    @Autowired
    private RedisOperator redisOperator;

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        try {
            log.info("======开始幂等性校验=========");
            var method = methodInvocation.getMethod();
            var annotation = method.getAnnotation(Idempotence.class);
            var paramName = annotation.paramName();
            var u = new LocalVariableTableParameterNameDiscoverer();
            var parameterNames = u.getParameterNames(method);
            var parameterList = Arrays.stream(parameterNames)
                    .filter(paramsName -> paramsName.equals(paramName)).collect(Collectors.toList());
            GraceException.isNull(parameterList, ResponseStatusEnum.IDEMPOTENCE_ERROR);
            var parameter = method.getParameters()[0];
            return methodInvocation.proceed();
        } finally {
            log.info("======幂等性校验结束=========");
        }
    }
}
